import csv
import pandas as pd
import numpy as np
import requests
import re
from commentGet import *
import csv


def cookieManager():
    file = open("cookie.csv", "r")
    df = pd.read_csv(file)
    data = np.array(df)
    print(data)
    for j in range(len(data)):
        headers = {
            "Referer": 'https://detail.tmall.com/item.htm?id=544636202128' ,
            "User-Agent": data[j][1],
            "cookie": data[j][0]
        }

        url = "https://rate.tmall.com/list_detail_rate.htm"
        urlparams = {
            "itemId": '544636202128',
            "sellerId": "3",
            "currentPage": str(1),
            "callback": "jsonp723"
        }
        r = requests.get(url, params=urlparams, headers=headers)
        match = re.search(r'"lastPage":(\d+)', r.text)
        if match is None:
           pass
        else:
            return headers
    print('储存所有cookie失效')#cookie弹窗
    return None
cookieManager()

def commentSort2(sg): #网址搜索
    url=input() #文本框输入
    list = commentSort(url)
    if list==[]:
        return #网址错误弹窗
    file = open("comment2.csv", "w", encoding="UTF8")
    csv_write=csv.writer(file)
    for j in range(len(list)):
        csv_write.writerow([str(0), url, '商品',list[j], list[j]])
    file.close
